<template>
  <div style="height:100%;">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" class="bg-white p-1">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
            v-model="queryParams.menuName"
            placeholder="请输入菜单名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8 my-1">
      <el-col :span="1.5">
        <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:menu:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table
        v-loading="searchLoading"
        :data="menuList"
        row-key="id"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        :header-cell-style="{background: '#DDD'}"
    >
      <el-table-column prop="menuName" label="名称" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="icon" label="图标" align="center" width="100">
        <template slot-scope="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序" width="60"></el-table-column>
      <el-table-column prop="perms" label="权限标识" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="component" label="组件路径" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="menuType" label="类型" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.menuType===1" type="info">{{ scope.row.menuType | filterMenuType }}</el-tag>
          <el-tag v-else-if="scope.row.menuType===2" type="success">{{ scope.row.menuType | filterMenuType }}</el-tag>
          <el-tag v-else-if="scope.row.menuType===3">{{ scope.row.menuType | filterMenuType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="hidden" label="隐藏" :show-overflow-tooltip="true" :formatter="hiddenFormat"></el-table-column>
      <el-table-column prop="remark" label="备注" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:menu:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="handleAdd(scope.row)"
              v-hasPermi="['system:menu:add']"
          >新增
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:menu:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table> -->

    <myTableSearch
      ref="myTableSearchRef"
      :isOperate="true"
      :operateWidth="180"
      :titleName="$route.meta.title"
      :queryFn="getList"
      :formData="formData"
      :tableLabel="tableLabel"
      @recombinationData="recombinationData"
      @successQuery="successQuery"
    >
      <template slot="tool">
        <div class="flex flex-x">
          <div class="fillBtn" @click="handleAdd" v-hasPermi="['system:user:add']">
            <img src="@/assets/images/gzq/icon-add.png" alt="" />
            <span>新增数据</span>
          </div>
        </div>
      </template>
      <template #hidden="scope">
        <span>{{hiddenFormat(scope.row)}}</span>
      </template>
      <template #icon="scope">
        <i :class="scope.row.icon"></i>
      </template>
      <template slot="operate" slot-scope="scope">
        <div class="operateBtn flex justify-center">
          <div class="itemBtn" @click="handleUpdate(scope.row)" v-hasPermi="['system:menu:edit']">修改</div>
          <div class="itemBtn ml-1" @click="handleAdd(scope.row)" v-hasPermi="['system:menu:add']">新增</div>
          <div class="itemBtn ml-1" @click="handleDelete(scope.row)" v-hasPermi="['system:menu:remove']">删除</div>
        </div>
      </template>
    </myTableSearch>

    <!-- 添加或修改菜单对话框 -->
    <myDialog :isDialog="open" :title="title" :width="'600px'" @handleClose="cancel">
      <template slot="content">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="上级菜单">
                <tree-select
                    v-model="form.parentId"
                    :options="menuOptions"
                    :normalizer="normalizer"
                    :show-count="true"
                    placeholder="选择上级菜单"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="类型" prop="menuType">
                <el-radio-group v-model="form.menuType">
                  <el-radio label="1">目录</el-radio>
                  <el-radio label="2">菜单</el-radio>
                  <el-radio label="3">按钮</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item v-if="form.menuType !== '3'" label="图标">
                <el-popover
                    placement="bottom-start"
                    width="460"
                    trigger="click">
                  <Icons @selected="selected" :icon="form.icon"></Icons>
                  <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标">
                    <i slot="prefix" class="el-icon-search el-input__icon" />
                  </el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="menuName">
                <el-input v-model="form.menuName" placeholder="请输入名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.menuType === '1' || form.menuType === '2' " label="路由" prop="path">
                <el-input v-model="form.path" placeholder="请输入路由"/>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="form.menuType === '2'"><!-- -->
              <el-form-item label="组件路径" prop="component">
                <el-input v-model="form.component" placeholder="请输入组件路径"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.menuType === '3'" label="权限标识">
                <el-input v-model="form.perms" placeholder="请权限标识" maxlength="50"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.menuType !== '3' " label="隐藏">
                <el-radio-group v-model="form.hidden">
                  <el-radio
                      v-for="dict in hiddenOptions"
                      :key="dict.dictValue"
                      :label="dict.dictValue"
                  >{{ dict.dictLabel }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.menuType === '2' " label="KEY">
                <el-input v-model="form.menuKey" placeholder="页面Name"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
      <template slot="footer">
        <div class="formBtnBox">
          <div class="saveBtn" @click="submitForm">确 定</div>
          <div class="editBtn ml-4"  @click="cancel">取 消</div>
        </div>
      </template>
    </myDialog>
  </div>
</template>

<script>
import {menuList, menuDetail, menuAdd, menuUpdate,menuDelete} from "@/api/sys/menu";
import TreeSelect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import Icons from "@/components/Icons";
export default {
  name: "Menu",
  components: { TreeSelect,Icons },
  data() {
    return {
      formData: [
        {
          label: "菜单名称",
          type: "input",
          key: "menuName",
        },
      ],
      tableLabel: [
        {
          prop: "menuName",
          label: "名称",
        },
        {
          prop: "icon",
          label: "图标",
          width: "100"
        },
        {
          prop: "sort",
          label: "排序",
          width: "100"
        },
        {
          prop: "perms",
          label: "权限标识",
        },
        {
          prop: "path",
          label: "路由",
        },
        {
          prop: "component",
          label: "组件路径",
        },
        {
          prop: "hidden",
          label: "隐藏",
          width: "60"
        }
      ],
      searchLoading: true,
      // 查询参数
      queryParams: {
        menuName: undefined,
        hidden: undefined
      },
      menuList: [],
      menuOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示状态数据字典
      hiddenOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        menuName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "顺序不能为空", trigger: "blur" }
        ],
        path: [
          { required: true, message: "路由地址不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getDicts("if_hidden").then((res)=>{
      this.hiddenOptions = res.data;
    })
    // this.getList();
  },
  methods: {
    getList(e) {
      return menuList(e)
    },
    //格式化搜索字段
    recombinationData(param, callback) {
      let params = {
        recombinationDataType:'queryParams',
        menuName:param.menuName
      }
      callback(params);
    },
    successQuery(e,callback){
      this.menuList = this.handleTree(e, "id", "parentId");
      callback(this.menuList)
    },
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    // getList() {
    //   this.loading = true;
    //   menuList(this.queryParams).then(res => {
    //     this.menuList = this.handleTree(res.data, "id", "parentId");
    //     this.searchLoading = false;
    //   });
    // },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.menuName,
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeSelect() {
      menuList({}).then(res => {
        this.menuOptions = [];
        const menu = { id: "0", menuName: '主类目', children: [] };
        menu.children = this.handleTree(res.data, "id", "parentId");
        this.menuOptions.push(menu);
      });
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd(row) {
      this.reset();
      this.getTreeSelect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加菜单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeSelect();
      menuDetail(row.id).then(res => {
        this.form = res.data;
        this.form.menuType = String(this.form.menuType);
        this.open = true;
        this.title = "修改菜单";
      });
    },
    // 显示状态字典翻译
    hiddenFormat(row) {
      if (row.menuType === 3) {
        return "";
      }
      return this.selectDictLabel(this.hiddenOptions, row.hidden);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        parentId: 0,
        menuName: undefined,
        menuKey: undefined,
        icon: undefined,
        menuType: "1",
        sort: 1,
        hidden: '0',
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            menuUpdate(this.form).then(() => {
              this.msgSuccess("修改成功");
              this.open = false;
              // this.getList();
              this.$refs.myTableSearchRef.searchReset();
            });
          } else {
            menuAdd(this.form).then(() => {
              this.msgSuccess("新增成功");
              this.open = false;
              // this.getList();
              this.$refs.myTableSearchRef.searchReset();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.menuName + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return menuDelete(row.id);
      }).then(() => {
        // this.getList();
        this.$refs.myTableSearchRef.searchReset();
        this.msgSuccess("删除成功");
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/deep/.el-radio__input.is-checked .el-radio__inner{
  background: #02a5ff;
}
/deep/.el-radio__input.is-checked+.el-radio__label{
    color: #02a5ff !important;
}
/deep/.el-radio__label{
  color: #eadbdb;
}

</style>
